﻿using AutoMapper;
using Enferno.Public.InversionOfControl;
using Enferno.Public.Web.Builders;
using Enferno.Public.Web.Mappers.ProductProfiles;
using Enferno.Public.Web.Models;
using Enferno.Public.Web.Repositories;
using Enferno.Public.Web.ViewModels;

namespace Enferno.Public.Web.Managers
{
    public class ProductManager : ProductWithCustomVariantManager<VariantModel>
    {
        public ProductManager(): base()
        {
            
        }
    }

    public class ProductWithCustomVariantManager<TVariantModel> : ProductBaseManager<ProductWithCustomVariantModel<TVariantModel>, TVariantModel, ProductManufacturerModel, ProductListCustomVariantModel<TVariantModel, ProductItemCustomVariantModel<TVariantModel>>, ProductItemCustomVariantModel<TVariantModel>, FilterModel, ProductFileModel, PriceModel, OnHandStatusModel, ParametricModel, ParametricValueModel, ProductViewModel>
        where TVariantModel :
            VariantBaseModel<OnHandStatusModel, PriceModel, ProductFileModel, ParametricModel, ParametricValueModel>
    {
        
    } 

    public class ProductBaseManager<TProductModel, TVariantModel, TProductManufacturerModel, TProductListModel, TProductItemModel, TFilterModel, TProductFileModel, TPriceModel, TOnHandStatusModel, TParametricModel, TParametricValueModel, TProductViewModel> : IProductManager<TProductModel, TProductViewModel>
        where TProductModel: ProductBaseModel<TVariantModel, TProductManufacturerModel, TProductListModel, TProductItemModel, TFilterModel, TProductFileModel, TPriceModel, TOnHandStatusModel, TParametricModel, TParametricValueModel>
        where TVariantModel: VariantBaseModel<TOnHandStatusModel, TPriceModel, TProductFileModel, TParametricModel, TParametricValueModel>
        where TProductManufacturerModel: ProductManufacturerModel
        where TOnHandStatusModel: OnHandStatusModel
        where TProductListModel: ProductListBaseModel<TProductItemModel, TFilterModel, TVariantModel, TProductFileModel, TProductManufacturerModel, TPriceModel, TOnHandStatusModel, TParametricModel, TParametricValueModel> 
        where TProductItemModel: ProductItemBaseModel<TVariantModel, TProductFileModel, TProductManufacturerModel, TPriceModel, TOnHandStatusModel, TParametricModel, TParametricValueModel>
        where TFilterModel: FilterModel
        where TProductFileModel: ProductFileModel, new()
        where TPriceModel: PriceModel
        where TParametricModel: ParametricBaseModel<TParametricValueModel>, new()
        where TParametricValueModel: ParametricValueModel, new()
    {
        protected readonly IProductRepository ProductRepository;
        protected readonly ProductBaseBuilder<TProductModel, TVariantModel, TProductManufacturerModel, TProductListModel, TProductItemModel, TFilterModel, TProductFileModel, TPriceModel, TOnHandStatusModel, TParametricModel, TParametricValueModel> ProductBuilder;

        public ProductBaseManager()
        {
            ProductRepository = IoC.Resolve<IProductRepository>();
            ProductBuilder = new ProductBaseBuilder<TProductModel, TVariantModel, TProductManufacturerModel, TProductListModel, TProductItemModel, TFilterModel, TProductFileModel, TPriceModel, TOnHandStatusModel, TParametricModel, TParametricValueModel>(IoC.Resolve<ISiteRules>());
        }

        public TProductViewModel GetProduct(int id)
        {
            var product = ProductRepository.GetProduct(id);
            var productModel = ProductBuilder.BuildProductModel(product);
            return Mapper.Map<TProductModel, TProductViewModel>(productModel);
        }
    }
}
